package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.dto.SocialRecruitmentChannelManagementAddDTO;
import com.oa.pm.dto.SocialRecruitmentChannelManagementListDTO;
import com.oa.pm.entity.SocialRecruitmentChannelManagement;
import com.oa.pm.mapper.SocialRecruitmentChannelManagementMapper;
import com.oa.pm.service.ISocialRecruitmentChannelManagementContractService;
import com.oa.pm.service.ISocialRecruitmentChannelManagementFileService;
import com.oa.pm.service.ISocialRecruitmentChannelManagementService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.SocialRecruitmentChannelManagementListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 社招渠道管理表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class SocialRecruitmentChannelManagementServiceImpl extends ServiceImpl<SocialRecruitmentChannelManagementMapper, SocialRecruitmentChannelManagement> implements ISocialRecruitmentChannelManagementService {

    private final SocialRecruitmentChannelManagementMapper socialRecruitmentChannelManagementMapper;

    @Autowired
    public SocialRecruitmentChannelManagementServiceImpl(SocialRecruitmentChannelManagementMapper socialRecruitmentChannelManagementMapper) {
        this.socialRecruitmentChannelManagementMapper = socialRecruitmentChannelManagementMapper;
    }

    @Autowired
    private ISocialRecruitmentChannelManagementContractService socialRecruitmentChannelManagementContractService;

    @Autowired
    private ISocialRecruitmentChannelManagementFileService socialRecruitmentChannelManagementFileService;

    @Override
    @CommonService
    public PageResult<SocialRecruitmentChannelManagement> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<SocialRecruitmentChannelManagement> list = socialRecruitmentChannelManagementMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<SocialRecruitmentChannelManagement> page = new Page<>(pageNum, pageSize);
            //查询
            List<SocialRecruitmentChannelManagement> list = socialRecruitmentChannelManagementMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public SocialRecruitmentChannelManagementListVO findSocialRecruitmentChannelManagementById(String id){
        return socialRecruitmentChannelManagementMapper.findSocialRecruitmentChannelManagementListDetails(id);
    }

    @CommonService
    @Override
    public PageResult<SocialRecruitmentChannelManagementListVO> findSocialRecruitmentChannelManagementList(SocialRecruitmentChannelManagementListDTO socialRecruitmentChannelManagementListDTO) {
        Page page = socialRecruitmentChannelManagementListDTO.getPage();
        return new PageResult<>(socialRecruitmentChannelManagementMapper.findSocialRecruitmentChannelManagementList(page, socialRecruitmentChannelManagementListDTO), page.getPages());
    }

    @Transactional
    @CommonService
    @Override
    public Resp add(SocialRecruitmentChannelManagementAddDTO socialRecruitmentChannelManagement) throws DataIsEmptyException {
        if(insert(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement())){
            socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementFile().setSocialRecruitmentChannelManagementId(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement().getId());
            socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementContract().setSocialRecruitmentChannelManagementId(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement().getId());
            if(socialRecruitmentChannelManagementContractService.insert(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementContract())
                    &&
                    socialRecruitmentChannelManagementFileService.insert(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementFile())){
                return new Resp().success("添加成功");
            }
        }
        throw new DataIsEmptyException("添加失败");
    }

    @Transactional
    @CommonService
    @Override
    public Resp edit(SocialRecruitmentChannelManagementAddDTO socialRecruitmentChannelManagement) throws DataIsEmptyException {
        if(updateById(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement())){
            socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementFile().setSocialRecruitmentChannelManagementId(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement().getId());
            socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementContract().setSocialRecruitmentChannelManagementId(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement().getId());
            socialRecruitmentChannelManagementContractService.delete(new EntityWrapper().eq("social_recruitment_channel_management_id", socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement().getId()));
            socialRecruitmentChannelManagementFileService.delete(new EntityWrapper().eq("social_recruitment_channel_management_id", socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagement().getId()));
            if(socialRecruitmentChannelManagementContractService.insert(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementContract())
                    &&
                    socialRecruitmentChannelManagementFileService.insert(socialRecruitmentChannelManagement.getSocialRecruitmentChannelManagementFile())){
                return new Resp().success("修改成功");
            }
        }
        throw new DataIsEmptyException("修改失败");
    }

    @Override
    public void export(SocialRecruitmentChannelManagementListDTO socialRecruitmentChannelManagementListDTO, HttpServletResponse response) {
        List<SocialRecruitmentChannelManagementListVO> socialRecruitmentChannelManagementListVOS;
        if(socialRecruitmentChannelManagementListDTO.getPage() != null){
            Page page = socialRecruitmentChannelManagementListDTO.getPage();
            socialRecruitmentChannelManagementListVOS = socialRecruitmentChannelManagementMapper.findSocialRecruitmentChannelManagementList(page, socialRecruitmentChannelManagementListDTO);
        }else{
            socialRecruitmentChannelManagementListVOS = socialRecruitmentChannelManagementMapper.findSocialRecruitmentChannelManagementList(new Page(), socialRecruitmentChannelManagementListDTO);
        }
        String[] rows = {"id", "平台名称", "网址", "合作人员", "联系电话", "合同到期时间", "提醒时间", "费用", "付款方式", "文件名称", "文件大小", "文件地址"};
        try {
            ExcelUtil.downloadExcel("社招平台列表" + DateUtil.getDayMonth(), rows, socialRecruitmentChannelManagementListVOS, "社招平台列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
